package com.sky.utils;


import com.sky.exception.ResultApi;
import com.sky.exception.SysException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * @Description: 时间工具类
 */
public class DateUtil  {
    // 时间戳
    public static long getTimeStamp() {
        Date d = new Date();
        //getTime()得到的是微秒， 需要换算成秒
        long timeStamp = d.getTime() / 1000;
        return timeStamp;
    }

    /**
     * 字符串转Date
     * @param str yyyy-MM-dd HH:mm:ss
     * @return
     * @throws Exception
     */
    public static Date stringToDate(String str,String format){
        if (format == null || format.equals("")) {
            format = "yyyy-MM-dd HH:mm:ss";
        }
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(str);
        }catch (Exception e){
            throw new SysException(ResultApi.DATE_ERROR);
        }
    }

    /**
     * 获取当前时间前几分钟
     * @return
     */
    public static String getCurrentTime(int x){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MINUTE, -x);// x分钟之前的时间
        Date beforeD = beforeTime.getTime();
        String time = sdf.format(beforeD);
        return time;
    }
    
    /**
             * 按照format格式化日期
     * @param format
     * @return String
     * @author	HuMengmeng
     * @date 2020年1月14日下午4:34:13
     */
    public static String dateToString(String format){
    	Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(date);
    }
    public static String dateToString(){
    	Date date = new Date();
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date);
    }
    public static String dateToString(Date date){
    	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    	return sdf.format(date);
    }
    public static String dateToString(Date date,String format){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return sdf.format(date);
    }
    public static String getPreDateToString(int month){
    	Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.MONTH,-month);
		Date date = calendar.getTime();
		return DateUtil.dateToString(date);
    }
    
    /**
          * 获取昨天日期
	* @return String
	*/
    public static String getYesterdayFormatDate(){
    	Calendar cal=Calendar.getInstance();
    	//获取昨天日期
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        SimpleDateFormat sp=new SimpleDateFormat("yyyy-MM-dd");
        return sp.format(d);
    }
    public static Date getYesterdayDate(){
    	Calendar cal=Calendar.getInstance();
    	//获取昨天日期
        cal.add(Calendar.DATE,-1);
        Date d=cal.getTime();
        return d;
    }

    /**
     * 获取当前周的第一天(周一)
     **/
    public static String getFirstDayOfWeek(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) ;
        if (dayOfWeek == 1) {//说明是个周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) - 6);
        }else{
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 1);
        }
        return format.format(cal.getTime());
    }

    /**
     * 获取当前周最后一天（周日）
     **/
    public static String getLastDayOfWeek(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        // 获得今天是一周的第几天，星期日是第一天，星期二是第二天......
        int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK) ;
        if (dayOfWeek == 1) {//说明是个周日
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE));
        }else{
            cal.set(Calendar.DAY_OF_WEEK, 1);//查出本周的周日
            cal.set(Calendar.DATE, cal.get(Calendar.DATE) + 7);
        }
        return format.format(cal.getTime());
    }

    /**
     * 获取当前月第一天
     **/
    public static String getCurrentMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 0);
        cale.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前月最后一天
     **/
    public static String getCurrentMonthLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        cale.add(Calendar.MONTH, 1);
        cale.set(Calendar.DAY_OF_MONTH, 0);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前年第一天
     **/
    public static String getCurrentYearFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        cale.clear();
        cale.set(Calendar.YEAR, currentYear);
        return format.format(cale.getTime());
    }

    /**
     * 获取当前年最后一天
     **/
    public static String getCurrentYearLastDay() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        int currentYear = cale.get(Calendar.YEAR);
        cale.clear();
        cale.set(Calendar.YEAR, currentYear);
        cale.roll(Calendar.DAY_OF_YEAR, -1);
        return format.format(cale.getTime());
    }

    public static List<String> getTodayHours() {
        Calendar cale = Calendar.getInstance();
        int currentHour = cale.get(Calendar.HOUR_OF_DAY);
        List<String> list = new ArrayList();
        if(currentHour > 10){
            for (int i = 10; i <currentHour ; i++) {
                list.add(String.valueOf(i));
            }
        }
        System.out.println(currentHour);
        return list;
    }

    /**
     * @Description 按照指定格式日期转字符串
     **/
    public static String dateToString2(Date date,String format){

        SimpleDateFormat sdf=new SimpleDateFormat(format);

        String str=sdf.format(date);

        return str;
    }

   /* public static void main(String[] args) {
        Calendar cale = Calendar.getInstance();
        int currentHour = cale.get(Calendar.HOUR_OF_DAY);
        List list = new ArrayList();
        if(currentHour > 10){
            for (int i = 10; i <currentHour ; i++) {
                list.add(i);
            }
        }
        System.out.println(getLastDayOfWeek(new Date()));
        System.out.println(earlierThan("2020-09-13 23:59:59","2020-09-14 23:59:59"));
    }*/
    /**
     * @Description 返回 2020-07: purchaseTime（2020-01-01）+depreciationPeriod（6个月）
     **/
    public static String getFormatDate(String purchaseTime, int depreciationPeriod) {
        Date date = stringToDate(purchaseTime,"yyyy-MM-dd");
        Calendar cal=Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, depreciationPeriod);
        Date endDate =cal.getTime();

        return dateToString2(endDate,"yyyy-MM");
    }

    public static boolean earlierThan(String time1, String time2) {
        Date date1 = stringToDate(time1,null);
        Date date2 = stringToDate(time2,null);
        Calendar cal1=Calendar.getInstance();
        Calendar cal2=Calendar.getInstance();
        cal1.setTime(date1);
        cal2.setTime(date2);
        return cal1.before(cal2);
    }

    public static List<String> getDateList(String begin,String end){
        List<String> dates = new ArrayList();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dBegin = null;
        Date dEnd = null;
        try {
            dBegin = sdf.parse(begin);
            dEnd = sdf.parse(end);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//4.将格式化后的第一天添加进集合
        dates.add(sdf.format(dBegin));
//5.使用本地的时区和区域获取日历
        Calendar calBegin = Calendar.getInstance();
//6.传入起始时间将此日历设置为起始日历
        calBegin.setTime(dBegin);
//8.判断结束日期是否在起始日历的日期之后
        while (dEnd.after(calBegin.getTime())) {
// 9.根据日历的规则:月份中的每一天，为起始日历加一天
            calBegin.add(Calendar.DAY_OF_MONTH, 1);
//10.得到的每一天就添加进集合
            dates.add(sdf.format(calBegin.getTime()));
//11.如果当前的起始日历超过结束日期后,就结束循环
        }
        return dates;
    }

}